package com.chinasoft.userservice.service.impl;

import com.chinasoft.ms.dto.UserDTO;
import com.chinasoft.ms.pojo.PageItem;
import com.chinasoft.ms.pojo.User;
import com.chinasoft.userservice.mapper.UserMapper;
import com.chinasoft.userservice.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class UserServiceImpl  implements UserService {

    UserMapper userMapper;



    /**
     *  进行用户账号和密码的校验
     * @param user：username和password
     * @return
     *   null： 登录失败
     *   user对象： 登录成功
     */
    @Override
    public User checkUser(User user) {
        // 执行查询
        User userDb = userMapper.selectByName(user.getUsername());

        if (userDb == null) {
            return null;
        }
        // 对密码进行判断
        if (user.getPassword().equals(userDb.getPassword())) {
            // 登录成功
            return userDb;
        }
        // 登录失败
        return null;
    }

    @Override
    public User getUserWithId(Long id) {

        return userMapper.selectById(id);
    }
    @Override
    public PageItem<UserDTO> queryByPageCond(Integer pageNum, Integer pageSize, User query) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.selectByCondition(query);
        PageInfo<User> pageInfo = new PageInfo<>(users);

        List<UserDTO> dtos = users.stream().map(this::convertToDTO).collect(Collectors.toList());

        PageItem<UserDTO> pageItem = new PageItem<>();
        pageItem.setList(dtos);
        pageItem.setTotal(pageInfo.getTotal());
        pageItem.setPageNum(pageInfo.getPageNum());
        pageItem.setPageSize(pageInfo.getPageSize());
        return pageItem;
    }

    @Override
    public boolean addUser(UserDTO userDTO) {
        User user = convertToEntity(userDTO);
        return userMapper.insert(user) > 0;
    }

    @Override
    public boolean updateUser(UserDTO userDTO) {
        User user = convertToEntity(userDTO);
        return userMapper.update(user) > 0;
    }

    @Override
    public boolean deleteUser(Long id) {
        return userMapper.deleteById(id) > 0;
    }

    @Override
    public boolean deleteBatch(List<Long> ids) {
        return userMapper.deleteBatch(ids) > 0;
    }

    @Override
    public boolean register(UserDTO userDTO) {
        try {
            // 检查用户名是否已存在
            User existingUser = userMapper.selectByName(userDTO.getUsername());
            if (existingUser != null) {
                System.out.println("用户名已存在: " + userDTO.getUsername());
                return false;
            }

            // 转换DTO为实体并插入数据库
            User user = convertToEntity(userDTO);
            int result = userMapper.insert(user);
            System.out.println("插入结果: " + result);
            return result > 0;
        } catch (Exception e) {
            System.out.println("注册异常: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Integer getUserRoleWithId(Long id) {
        return userMapper.getUserRoleWithId(id);
    }

    private UserDTO convertToDTO(User user) {
        if (user == null) {
            return null;
        }
        UserDTO dto = new UserDTO();
        dto.setId(user.getId());
        dto.setUsername(user.getUsername());
        dto.setPassword(user.getPassword());
        dto.setNickName(user.getNickName());
        dto.setPhone(user.getPhone());
        dto.setSex(user.getSex());
        dto.setAddress(user.getAddress());
        dto.setRole(user.getRole());
        return dto;
    }

    private User convertToEntity(UserDTO dto) {
        if (dto == null) {
            return null;
        }
        User user = new User();
        user.setId(dto.getId());
        user.setUsername(dto.getUsername());
        user.setPassword(dto.getPassword());
        user.setNickName(dto.getNickName());
        user.setPhone(dto.getPhone());
        user.setSex(dto.getSex());
        user.setAddress(dto.getAddress());
        user.setRole(dto.getRole());
        return user;
    }
}
